package com.market.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Product;
import com.market.domain.Role;
import com.market.domain.Supplier;
import com.market.domain.User;
import com.market.manager.SupplierManager;
import com.market.util.CollectionUtil;
import com.market.web.model.ProductModel;
import com.market.web.model.SupplierModel;


@Controller("supplierController")
@Scope("session")
public class SupplierController extends GenericController<SupplierManager, SupplierModel, Supplier> {
	@Autowired
	private ProductModel productModel;
	
	/*@Override
	public Supplier getNewObject() {
		Supplier supplier = new Supplier();
		supplier.setIsNew(true);
		return supplier;
	}*/
    @Autowired
	@Override
	protected void setModel(SupplierModel supplierModel) {
		this.model = supplierModel;		
	}
    @Autowired
	@Override
	protected void setManager(SupplierManager supplierManager) {
		this.manager = supplierManager;
		
	}
    

	/**
	 * Add all selected products to current supplier
	 * And clear selected propertied after this operation finish
	 * 
	 */
	public void addSelectedProductsToCurrent() {
		Supplier current = this.getModelCurrent();
		List<Product> selectedProducts = productModel.getSelecteds();
		CollectionUtil<Product> util = new CollectionUtil<Product>();
		util.addAll(current.getProducts(), selectedProducts);
		util.clearSelection(selectedProducts);  //Normally we have to clear selected properties, after operation finish
	}
	
	/**
	 * Remove all selected roles from current user
	 * And clear selected propertied after this operation finish
	 * 
	 */

	public void removeSelectedProductsFromCurrent() {
		List<Product> products = this.getModel().getCurrent().getProducts();
		CollectionUtil<Product> util = new CollectionUtil<Product>();
		List<Product> unSelectedProducts = util.extractUnSelected(products);
		this.getModelCurrent().setProducts(unSelectedProducts);
		util.clearSelection(products);		//Normally we have to clear selected properties, after operation finish
	}

}
